package com.example.mywechatprogram.service;

import com.example.mywechatprogram.entity.Response;
import com.example.mywechatprogram.entity.Word;
import com.example.mywechatprogram.mapper.ExamMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ExamService {
    @Autowired
    ExamMapper examMapper;
    public Response add(int word_id, String word, String userId){
        try {
            int index=examMapper.add(userId,word,word_id);
            if(index==1)return new Response(Response.SERVICE_RUN_SUCCESSFULLY,null,"添加成功");
            else return new Response(Response.SERVICE_RUN_FIELD,null,"添加失败");
        }catch (DuplicateKeyException e){
            return new Response(Response.SERVICE_RUN_FIELD,null,"单词已存在");
        }catch (Exception e){
            return new Response(Response.SERVICE_RUN_FIELD,null,"内部错误");
        }
    }
    public Response request(String userId){
        try {
            List<Word> list=examMapper.request(userId);
            if(list!=null)return new Response(Response.SERVICE_RUN_SUCCESSFULLY,list,"查询成功");
            else if(list==null)return new Response(Response.SERVICE_RUN_SUCCESSFULLY,null,"自定义单词库为空");
            else return new Response(Response.SERVICE_RUN_FIELD,null,"内部错误");
        }catch (Exception e){
            return new Response(Response.SERVICE_RUN_FIELD,null,"内部错误");
        }
    }

    public Response delCustom(String userId,int word_id){
        try {
            int i=examMapper.del(userId,word_id);
            if(i==1)return new Response(Response.SERVICE_RUN_SUCCESSFULLY,null,"删除成功");
            else return new Response(Response.SERVICE_RUN_FIELD,null,"单词不存在");
        }catch (Exception e){
            System.out.println(e.getMessage());
            return new Response(Response.SERVICE_RUN_FIELD,null,"内部错误");
        }
    }
    public Response get3(){
        try {
            List <Word> res;
            res = examMapper.get3();
            return new Response(Response.SERVICE_RUN_SUCCESSFULLY,res,"随机三个单词");
        }catch (Exception e){
            return new Response(Response.SERVICE_RUN_FIELD,null,"内部错误");
        }
    }
}
